package com.zbkj.service.service.impl;

import com.zbkj.common.request.PageParamRequest;
import com.github.pagehelper.PageHelper;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zbkj.common.exception.AppException;
import com.zbkj.common.response.AgentConfigItemsPageDataResponse;
import com.zbkj.common.request.AgentConfigItemsSearchRequest;
import com.zbkj.common.request.AgentConfigItemsSaveRequest;

import com.zbkj.common.result.CommonResultCode;
import com.zbkj.common.utils.CrmebDateUtil;
import com.zbkj.common.model.agent.AgentConfigItems;
import com.zbkj.service.dao.AgentConfigItemsDao;
import com.zbkj.service.service.AgentConfigItemsService;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* @author dazongzi
* @description AgentConfigItemsServiceImpl 接口实现
* @date 2025-07-01
*/
@Service
public class AgentConfigItemsServiceImpl extends ServiceImpl<AgentConfigItemsDao, AgentConfigItems> implements AgentConfigItemsService {

    @Resource
    private AgentConfigItemsDao dao;

    @Override
    public Boolean create(AgentConfigItemsSaveRequest agentConfigItemsSaveRequest) {
        AgentConfigItems agentConfigItems = new AgentConfigItems();
        BeanUtils.copyProperties(agentConfigItemsSaveRequest, agentConfigItems, "id");
        boolean save = save(agentConfigItems);
        if (!save) {
            throw new AppException(CommonResultCode.VALIDATE_FAILED, "添加失败");
        }
        return save;
    }

    /**
     * 分页查询平台代理配置表
     *
     * @param request          请求参数
     * @param pageParamRequest 分页类参数
     * @return List<AgentConfigItems>
     * @author dazongzi
     * @since 2025-07-01
     */
    @Override
    public List<AgentConfigItemsPageDataResponse> getList(AgentConfigItemsSearchRequest request, PageParamRequest pageParamRequest) {
        PageHelper.startPage(pageParamRequest.getPage(), pageParamRequest.getLimit());

        Date currentDate = CrmebDateUtil.nowDateTime();

        //带 AgentConfigItems 类的多条件查询
        LambdaQueryWrapper<AgentConfigItems> lambdaQueryWrapper = Wrappers.lambdaQuery();
        // type 类型不能为空 bean中已经限制
        /*
        lambdaQueryWrapper.eq(AgentConfigItems::getType, request.getType());

        if (ObjectUtil.isNotEmpty(request.getId()) && request.getId() > 0) {
            lambdaQueryWrapper.eq(AgentConfigItems::getId, request.getId());
        }
        if (ObjectUtil.isNotEmpty(request.getName())) {
            lambdaQueryWrapper.like(AgentConfigItems::getName, URLUtil.decode(request.getName()));
        }

        if (ObjectUtil.isNotEmpty(request.getStatus())) {
            lambdaQueryWrapper.eq(AgentConfigItems::getStatus, request.getStatus());
        }
        if (ObjectUtil.isNotEmpty(request.getMethod())) {
            lambdaQueryWrapper.eq(AgentConfigItems::getMethod, request.getMethod());
        }*/

        // 时间区间
        //if (ObjectUtil.isNotEmpty(request.getStarttime()) && ObjectUtil.isNotEmpty(request.getEndtime())) {
        //    lambdaQueryWrapper.between(AgentConfigItems::getCreatetime, request.getStarttime(), request.getEndtime());
        //}
        // * -1 已结束 结束时间小于当前时间
        // * 1 进行中 结束时间大于当前时间 && 开始时间小于等于当前时间
        // * 0 未开始 开始时间大于当前时间
        /*
        if (ObjectUtil.isNotEmpty(request.getRunningStatus())) {
            switch (request.getRunningStatus()) {
                case -1:
                    lambdaQueryWrapper.lt(AgentConfigItems::getEndtime, currentDate);
                    break;
                case 0:
                    lambdaQueryWrapper.gt(AgentConfigItems::getStarttime, currentDate);
                    break;
                case 1:
                    lambdaQueryWrapper.ge(AgentConfigItems::getEndtime, currentDate).le(AgentConfigItems::getStarttime, currentDate);
                    break;
            }
        }*/

        lambdaQueryWrapper.orderByDesc(AgentConfigItems::getCreateTime);
        List<AgentConfigItems> agentConfigItemss = dao.selectList(lambdaQueryWrapper);
        // 添加活动状态
        List<AgentConfigItemsPageDataResponse> agentConfigItemsResponses = new ArrayList<>();

        agentConfigItemss.stream().forEach(x->{
            AgentConfigItemsPageDataResponse agentConfigItemsResponse = new AgentConfigItemsPageDataResponse();
            BeanUtils.copyProperties(x, agentConfigItemsResponse);
            //agentConfigItemsResponse.setRunningStatus(x.getStarttime(), x.getEndtime(), currentDate);
            agentConfigItemsResponses.add(agentConfigItemsResponse);
        });
        return agentConfigItemsResponses;
    }

    @Override
    public void deleteByConfigId(List<Integer> configId) {
        remove(Wrappers.<AgentConfigItems>lambdaQuery().in(AgentConfigItems::getConfigId, configId));
    }

    @Override
    public List<AgentConfigItems> getListByConfigId(Integer id) {
        LambdaQueryWrapper<AgentConfigItems> lqw = Wrappers.lambdaQuery();
        lqw.eq(AgentConfigItems::getConfigId, id);
        lqw.eq(AgentConfigItems::getIsDel, false);
        lqw.orderByAsc(AgentConfigItems::getSort);
        return dao.selectList(lqw);
    }

    @Override
    public List<AgentConfigItems> getListByConfigIdPlatform(Integer id) {
        LambdaQueryWrapper<AgentConfigItems> lqw = Wrappers.lambdaQuery();
        lqw.eq(AgentConfigItems::getConfigId, id);
        lqw.eq(AgentConfigItems::getIsDel, false);
        lqw.eq(AgentConfigItems::getBrandId, 0);
        lqw.orderByAsc(AgentConfigItems::getSort);
        return dao.selectList(lqw);
    }

    @Override
    public AgentConfigItems getByConfigId(Integer id, Integer agentConfigCategoryId) {
        LambdaQueryWrapper<AgentConfigItems> lqw = Wrappers.lambdaQuery();
        lqw.eq(AgentConfigItems::getConfigId, id);
        lqw.eq(AgentConfigItems::getCategoryId, agentConfigCategoryId);
        lqw.eq(AgentConfigItems::getIsDel, false);
        lqw.orderByAsc(AgentConfigItems::getSort);
        return getOne(lqw);
    }


}

